package org.scratchcrew.services;

import org.scratchcrew.domain.Board;
import org.scratchcrew.domain.ScratchFile;
import org.scratchcrew.domain.dao.BoardDao;
import org.scratchcrew.domain.dao.ScratchFileDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("scratchFileService")
public class ScratchFileServiceImpl implements ScratchFileService {
	@Autowired
	private ScratchFileDao scratchFileDao;
	@Autowired
	private BoardDao boardDao;

	@Override
	public ScratchFile create(Long boardId, String fileName, String mimeType,
			byte[] fileContent) {
		Board board = boardDao.find(boardId);
		ScratchFile scratchFile = new ScratchFile();
		scratchFile.setBoard(board);
		scratchFile.setFileName(fileName);
		scratchFile.setMimeType(mimeType);
		scratchFile.setScratchFile(fileContent);
		scratchFile.setLastModifiedDate(System.currentTimeMillis());
		return scratchFileDao.save(scratchFile);
	}

	@Override
	public void delete(Long id) {
		ScratchFile scratchFile = scratchFileDao.find(id);
		scratchFileDao.delete(scratchFile);
	}

	@Override
	public ScratchFile find(Long id) {
		ScratchFile scratchFile = scratchFileDao.find(id);
		return scratchFile;
	}

}
